/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package th.stripes.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author fikri
 */
public class FTPUtil {
    
    private static Logger LOGGER = LoggerFactory.getLogger(FTPUtil.class);
    
    public void downloadAndRemove(String username, String password, String serverAdd, String remoteAddr
            , String localAddr, String remoteArchiveAddr) {
        FTPClient ftp = new FTPClient();
        int reply;
        String tmp = System.getProperty("java.io.tmpdir") + File.separator + "sms_info";
        LOGGER.info("temp : {}", tmp);
        InputStream is = null;
        try {
            
            LOGGER.debug(" server add {}", serverAdd);
            LOGGER.debug(" username {}", username);
            LOGGER.debug(" password {}", password);

            ftp.connect(serverAdd);
            ftp.login(username, password);
            reply = ftp.getReplyCode();
            
            LOGGER.debug("reply {}", reply);
           

            if (FTPReply.isPositiveCompletion(reply)) {
                LOGGER.info("Connected Success to {}", serverAdd);

                FTPFile[] ftpFiles = ftp.listFiles("/sms_data/");
//                FTPFile[] ftpFiles = ftp.listFiles();
                LOGGER.info("files available {}" + String.valueOf(ftpFiles.length) );
                for (FTPFile ftpFile : ftpFiles) {
                    if (ftpFile.isDirectory() || !ftpFile.getName().endsWith("txt") || ftpFile.getSize() == 0) continue;
                    String fileName = ftpFile.getName();
                    LOGGER.info("file name {}", fileName);
                    ftp.retrieveFile("/sms_data/" + fileName,new FileOutputStream(new File(localAddr + fileName)));
                    is = new FileInputStream( new File(localAddr + fileName));
                    LOGGER.info("saving inside {}", remoteArchiveAddr);
                    ftp.storeFile("/sms_data/arc/" + fileName, is);
                    ftp.deleteFile("/sms_data/" + fileName);                    
                }   

            } else {
                LOGGER.info("Connection to {} Failed ", serverAdd);
                ftp.disconnect();
            }

        } catch (SocketException ex) {
            LOGGER.info("ERROR {}" , ex.getMessage());
        } catch (IOException ex) {
            LOGGER.info("ERROR {}" , ex.getMessage());
        }finally {
            LOGGER.info("finally");
            try {
                ftp.disconnect();
                if ( is != null) {
                    is.close();
                }
            }catch (Exception ex) {
                LOGGER.info(ex.getMessage());
            }
            
        }
    }
    
    public static void main (String[] args) {
        new FTPUtil().downloadAndRemove("Administrator", "P@ssw0rd", "210.187.87.170", "D:\\NGEIP\\cygwin\\tmp\\sms_data\\", 
                "/home/fikri/Works/Projects/tmp/sms/incoming/", "D:\\NGEIP\\cygwin\\tmp\\sms_data\\arc\\");
    }
}
